40923227 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • About
    • Develop
      • test
  • HomeWork
    • HW1
      • Overview
      • AGP Slots (AGP插槽)
      • Processors(處理器)
      • Video Displays and Video Cards(顯示器與顯示卡)
      • Video Output
      • Install and Configure Expansion Cards(安裝和配置擴充卡)
      • Storage Devices Overview(存儲設備概述)
      • Storage Devices Hard Disk Drives(儲存設備 硬碟驅動器)
      • Storage Devices  Hot Swapping(儲存設備 熱交換)
      • Heat Sources(熱源)
      • heat reduction
      • BIOS Settings(bios設置)
    • HW2
      • 作業2-1
      • 變數 Variables
      • HTML Tutorial HTML教程
    • HW3
      • Check Tic Tac Toe
      • Max Of Three
      • Tic Tac Toe Game
HW3 << Previous Next >> Max Of Three

Check Tic Tac Toe

This exercise is Part 2 of 4 of the Tic Tac Toe exercise series. The other exercises are: Part 1, Part 3, and Part 4.

As you may have guessed, we are trying to build up to a full tic-tac-toe board. However, this is significantly more than half an hour of coding, so we’re doing it in pieces.

您可能已經猜到了,我們正在努力建立完整的井字遊戲板。但是,這遠遠超過了半個小時的編碼,因此我們要分批進行。

Today, we will simply focus on checking whether someone has WON a game of Tic Tac Toe, not worrying about how the moves were made.

我們只關注檢查某人是否贏得了井字遊戲,而不用擔心這些動作是如何進行的。

If a game of Tic Tac Toe is represented as a list of lists, like so:

game = [[1, 2, 0],
	[2, 1, 0],
	[2, 1, 1]]


where a 0 means an empty square, a 1 means that player 1 put their token in that space, and a 2 means that player 2 put their token in that space.

Your task this week: given a 3 by 3 list of lists that represents a Tic Tac Toe game board, tell me whether anyone has won, and tell me which player won, if any. A Tic Tac Toe win is 3 in a row - either in a row, a column, or a diagonal. Don’t worry about the case where TWO people have won - assume that in every board there will only be one winner.

給定一個代表Tic Tac Toe遊戲板的3 x 3列表,請告訴我是否有人贏了,並告訴我哪
個玩家贏了(如果有)。井字遊戲的勝利是連續3個-連續,一列或對角線。不必擔心會
有兩個人獲勝的情況-假設在每個董事會中只有一個獲勝者。

Here are some more examples to work with:

winner_is_2 = [[2, 2, 0],
	[2, 1, 0],
	[2, 1, 1]]

winner_is_1 = [[1, 2, 0],
	[2, 1, 0],
	[2, 1, 1]]

winner_is_also_1 = [[0, 1, 0],
	[2, 1, 0],
	[2, 1, 1]]

no_winner = [[1, 2, 0],
	[2, 1, 0],
	[2, 1, 2]]

also_no_winner = [[1, 2, 0],
	[2, 1, 0],
	[2, 1, 0]]


Topics

This exercise is challenging, but doable with only lists (of lists)! Lists of lists are nearly the same as lists, just a bit trickier. Remember that to get the first element in a list called my_list = [5, 10, 15], you index it with a variable like so:

僅可使用列表中的列表!列表的列表與列表幾乎相同,只是比較棘手。請記住,要獲取
名為my_list = [5,10,15]的列表中的第一個元素,請使用如下所示的變量對其進
行索引:
>>> print(my_list[0])
	5
	>>> print(my_list[-1])
	15
	>>> print(len(my_list))
	3

When working with lists of lists, it is the same! Say you have a list matrix = [[1, 2], [3, 4]]. Then, take a look at this:

>>> print(matrix[0])
	[1, 2]
	>>> print(matrix[-1])
	[3, 4]
	>>> first_row = matrix[0]
	>>> print(first_row[0])
	1
	>>> print(matrix[0][0])
	1
	>>> print(matrix[1][1])
	4

The cool part is that you can use double-indexing to read the elements from our list of lists matrix! By indexing matrix[0][1] we are saying give me the 1st element of the 0th element of matrix, which in this case is 2.最酷的部分是您可以使用雙索引從列表列表矩陣中讀取元素!通過索引矩陣[0] [1],我們要給我矩陣第0個元素的第一個元素,在這種情況下為2。


HW3 << Previous Next >> Max Of Three

Copyright © All rights reserved | This template is made with by Colorlib